"""
实验模板数据模型
"""
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from database import Base


class ExperimentTemplate(Base):
    """实验模板表"""
    __tablename__ = "experiment_templates"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, index=True)
    description = Column(Text, nullable=True)
    yaml_content = Column(Text, nullable=False)
    json_config = Column(Text, nullable=False)
    tags = Column(Text, nullable=True)  # JSON 数组字符串
    version = Column(Integer, nullable=False, default=1)
    parent_version_id = Column(Integer, ForeignKey("experiment_templates.id"), nullable=True)
    application_id = Column(Integer, ForeignKey("applications.id"), nullable=False, index=True, comment="所属应用ID")
    change_log = Column(Text, nullable=True)
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    deleted_at = Column(DateTime(timezone=True), nullable=True)
    created_by = Column(String(100), nullable=True)

    def __repr__(self):
        return f"<ExperimentTemplate(id={self.id}, name='{self.name}', version={self.version})>"
